package cn.nc.controller;


import cn.nc.entity.Article;
import cn.nc.entity.Comment;
import cn.nc.entity.User;
import cn.nc.repository.ArticleRepository;
import cn.nc.repository.CommentRepository;
import cn.nc.service.ArticleService;
import cn.nc.service.CommentService;
import cn.nc.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Controller
public class IndexController {

    @Autowired
    UserService userService;
    @Autowired
    ArticleService articleService;
    @Autowired
    CommentService commentService;
    @Autowired
    ArticleRepository articleRepository;
    @Autowired
    CommentRepository commentRepository;


    @GetMapping("/")
    public String index(Model model){
        return page(1,model);
    }

    //主页  分页显示
    @GetMapping("/page/{page}")
    public String page(@PathVariable("page")int page,Model model){
        //每页文章数
        int size = 8;
        Page<Article> articles = articleService.pageArticle(page-1,size);
        model.addAttribute("page",page);
        model.addAttribute("totalPage",articles.getTotalPages());
        model.addAttribute("articles",articles.getContent());
        System.out.println(articles);
        return "client/index";
    }


    //个人用户发表文章分页显示
    @GetMapping("page/user/{page}")
    public String userPage(@PathVariable("page")int page, Model model){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

//        List<Article> articles = articleService.getPageData(1,5,((User)authentication.getPrincipal()).getId());
//        model.addAttribute("page",page);
//        model.addAttribute("totalPage",2);
//        model.addAttribute("articles",articles);
        Integer userId = ((User)authentication.getPrincipal()).getId();
        Specification<Article> spec = new Specification<Article>() {
            @Override
            public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                return query.where(builder.equal(root.get("user").get("id"),userId)).getRestriction();
            }
        };

        List<Article> articles = null;

        PageRequest pageRequest = PageRequest.of(page-1,5);
        Page<Article> articlePage = null;

        articlePage = articleRepository.findAll(spec,pageRequest);
        System.out.println(articlePage);
        model.addAttribute("page",page);
        model.addAttribute("totalPage",articlePage.getTotalPages());
        model.addAttribute("articles",articlePage.getContent());


        System.out.println(page);
        System.out.println(articlePage.getTotalPages());
        System.out.println(articlePage.getContent());


        return "client/userArticle";
    }






    //详情页面
    @GetMapping("/article/{id}")
    public String article(@PathVariable("id")int id,Model model){
        Article article = articleService.findArticleById(id);
        List<Comment> comments = commentService.findCommentByArticleId(id);
        model.addAttribute("article",article);
        model.addAttribute("comments",comments);
        return "client/article";
    }


    //发表评论
    @PostMapping("article/{id}/comment")
    public String comment(@PathVariable("id")int id,String author,String content){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User)authentication.getPrincipal();

        commentService.addComment(id,author,content);
        return "redirect:/article/"+id;

    }

    //删除评论
    @GetMapping("article/{id}/delete")
    public String deleteComment(@PathVariable("id")int id){
        Comment comment = commentRepository.findById(id).get();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User)authentication.getPrincipal();

        if(user.getId()==comment.getArticle().getUser().getId()){
            commentRepository.deleteById(id);
        }else{
            return "redirect:/article/"+comment.getArticle().getId();
        }


        return "redirect:/article/"+comment.getArticle().getId();

    }
}
